/**
 * Created by 671643387 on 5/23/2017.
 */

var e_Pattern = require('./Poker.js').enum_Pattern
var poker=require('./Poker.js')


module.exports= {
    _Pattern:0,
    arr_sort : function (i_Arr) {
        for (var i = 0; i < i_Arr.length; i++) {
            for (var j = i; j < i_Arr.length; j++) {
                if (i_Arr[i] > i_Arr[j]) {
                    var tmp = i_Arr[i];
                    i_Arr[i] = i_Arr[j];
                    i_Arr[j] = tmp;
                }//end if
            }//end for
        }//end for
    },

    b_isBomb : function (arr_Hand) {

        var arrCards = [3];
        var i = 0;
        ExObject.eachKeyNum(arr_Hand, function (k, v) {
            if (v != 0) {
                arrCards[i] = poker.getPoint(k);
                i++;
            }//end if
        })
        if (i != 3)
            return false;
        var lastPoint = 0;
        for (var i = 0; i < 3; i++) {
            var tmpPoint = arrCards[i];
            if (lastPoint != 0 && lastPoint != tmpPoint)
                return false;
            lastPoint = tmpPoint;
        }
        return true;
        /*
         var tmp=0;
         for(var i= 0,len=arrCards.length;i<len;i++)
         {
         var CardValue=arrCards[i];
         var point = poker.getPoint(CardValue);
         if(tmp!=0&&tmp!=point){
         return false;
         }
         tmp=point;
         }return true;
         */
    },

     b_isFlush : function (arr_Hand) {
        if (false == b_isGolden(arr_Hand))
            return false;
        if (false == b_isTractor(arr_Hand))
            return false;
        return true;
    },

     b_isGolden : function (arr_Hand) {
        var arrCards = [3];
        var i = 0;
        ExObject.eachKeyNum(arr_Hand, function (k, v) {
            if (v != 0) {
                arrCards[i] = poker.getColor(k);
                i++;
            }//end if
        })
        if (i != 3)
            return false;
        var lastColor = 0;
        for (var i = 0; i < 3; i++) {
            var tmpColor = arrCards[i];
            if (0 != lastColor && lastColor != tmpColor) {
                return false;
            }
            lastColor = tmpColor;
        }
        return true;
    },

     b_isTractor : function (arr_Hand) {
        var arrCards = [3];
        var i = 0;
        ExObject.eachKeyNum(arr_Hand, function (k, v) {
            if (v != 0) {
                arrCards[i] = poker.getPoint(k);
                i++;
            }//end if
        })
        if (i != 3)
            return false;
        this.arr_sort(arrCards);
        if (1 != arrCards[1] - arrCards[0] ||
            1 != arrCards[2] - arrCards[1]) {
            if (arrCards[0] != 2 && arrCards[1] != 13    // K A 2
                && arrCards[2] != 14) {
                if (arrCards[0] != 2 && arrCards[1] != 3
                    && arrCards[2] != 14) {
                    return false;
                }
            }
        }
        else return true;
    },


     b_isPair : function (arr_Hand) {
        var arrCards = [3];
        var i = 0;
        ExObject.eachKeyNum(arr_Hand, function (k, v) {
            if (v != 0) {
                arrCards[i] = poker.getPoint(k);
                i++;
            }//end if
        })
        if (i != 3)
            return false;
        if (arrCards[0] == arrCards[1]
            || arrCards[0] == arrCards[2]
            || arrCards[1] == arrCards[2]) {
            return true;
        }
    },

     b_isNormal : function (arr_Hand) {

    },

     b_isDragon : function (arr_Hand) {
         var arrCards=[3];
         var arrColor=[3];
         var i=0;

         ExObject.eachKeyNum(arr_Hand,function(k,v){
             if(0!=v)
             arrCards[i]=poker.getPoint(k);
             arrColor[i]=poker.getColor(k);
             i++;
         })
         this.arr_sort(arrCards);
         // this combination has 3 different color
         if(arrColor[0]!=arrColor[1]&&arrColor[0]!=arrColor[2]
         &&arrColor[1]!=arrColor[2]){
             if(2==arrCards[0]&&3==arrCards[1]&&5==arrCards[2]){
                 return true;
             }//end if
         }//end if
         else return false;
    },

    CompareNormal:function(oPlayer1,oPlayer2){
         var arr_Hands1=this._getHandPointArray(oPlayer1);
        var  arr_Hands2=this._getHandPointArray(oPlayer2);
        this.arr_sort(arr_Hands1);
        this.arr_sort(arr_Hands2);
        var i=3;
        while(i>=0){
            if(arr_Hands1[i]>arr_Hands2[i]){
                return oPlayer1;
            }else if(arr_Hands2[i]>arr_Hands1[i]){
                return oPlayer2;
            }else if(oPlayer1.getIndex()>oPlayer2.getIndex()){
                return oPlayer2;
            }
            else return oPlayer1;
            i--;
        }//end while
    },

    //Get each cards' point array
    _getHandPointArray:function(cards){
        var arr=[]
        var i=0
        ExObject.eachKeyNum(cards,function(k,v){
            if(v!=0){
                arr[i]=poker.getPoint(k);
                i++;
            }
        })
        return arr;
    },

   GetPattern : function (arr_Hand) {

    if (this.b_isBomb(arr_Hand)) {
        return this._Pattern = e_Pattern.BOMB;
    }//end if
    if (this.b_isDragon(arr_Hand)) {
        return this._Pattern = e_Pattern.DRAGON;
    }//end if
    if (this.b_isFlush(arr_Hand)) {
        return this._Pattern = e_Pattern.FLUSH;
    }//end if
    if (this.b_isGolden(arr_Hand)) {
        return this._Pattern = e_Pattern.GOLDEN;
    }//end if
    if (this.b_isPair(arr_Hand)) {
        return this._Pattern = e_Pattern.PAIR;
    }
    if (this.b_isTractor(arr_Hand)) {
        return this._Pattern = e_Pattern.TRACTOR;
    }
    return this._Pattern = e_Pattern.NORMAL;
},

}